package com.service.policyidservice.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.controller.vo.PolicyInfoVo;
import com.mapper.InsuranceMapper;
import com.mapper.InsuredMapper;
import com.mapper.PolicyMapper;
import com.pojo.Insurance;
import com.pojo.Insured;
import com.pojo.InsuredExample;
import com.pojo.InsuredExample.Criteria;
import com.pojo.Policy;
import com.pojo.PolicyExample;

import com.service.policyidservice.Policyservice;
@Service
public class PolicyidserviceImpl implements Policyservice{
    @Autowired
    private PolicyMapper policyMapper;
	@Autowired
	private InsuredMapper insuredMapper;
	@Autowired
	private InsuranceMapper insuranceMapper;
	
	@Override
	public List<Policy> selectByExample(PolicyExample example) {
		
		return policyMapper.selectByExample(example);
	}
    
	
	//带条件查询保单信息（根据被保人姓名）返回集合
	@Override
	public List<PolicyInfoVo> getPolicyInfoVoList(String insuredname) {
		 
		//创建一个保单的VO类集合
				List<PolicyInfoVo> listInfo = new ArrayList<PolicyInfoVo>();
		 //判断是否输入被保人姓名
				  if (insuredname!=null&&!"".equals(insuredname)) {
					  //输入了姓名按姓名查询并返回集合
					  InsuredExample insuredExample = new InsuredExample();
					  Criteria criteria = insuredExample.createCriteria();
					  criteria.andInsurednameEqualTo(insuredname);
					  List<Insured> list = insuredMapper.selectByExample(insuredExample);
					  if (list!=null) {
							//循环得到的被保人集合
							  for (int i = 0; i < list.size(); i++) {
								  
								  //按循环依次得到被保人的姓名，电话，身份证
								  String identity = list.get(i).getIdentity();
								  String insuredphone = list.get(i).getInsuredphone();
								  String insuredname2 = list.get(i).getInsuredname();
								  //通过被保人身份证得到保单表信息集合
								  PolicyExample policyExample = new PolicyExample();
								  com.pojo.PolicyExample.Criteria criteria2 = policyExample.createCriteria();
								  criteria2.andIdentityEqualTo(identity);
								 
								  List<Policy> list2 = policyMapper.selectByExample(policyExample);
								  if (list2!=null&&list2.size()>0) {
									  
									  
									  for (int j = 0; j < list2.size(); j++) {
										  //初始化一个VO类
										  PolicyInfoVo policyInfoVo = new PolicyInfoVo();
										  //初始化保单表定义为空
										  Policy policy=null;
										  //判断是否为空再赋值
										  policy=list2.get(j);
										  //得到保单类型id
										  Integer insuranceid = policy.getInsuranceid();
										  //通过id得到保险类型
										  Insurance insurance = insuranceMapper.selectByPrimaryKey(insuranceid);
										  //将得到的值一次赋予VO类
										  policyInfoVo.setInsure(policy.getInsure());
										  policyInfoVo.setPolicyno(policy.getPolicyno());
										  policyInfoVo.setInsuredname(insuredname2);
										  policyInfoVo.setInsuredphone(insuredphone);
										  policyInfoVo.setInsurancetype(insurance.getInsurancetype());
										  policyInfoVo.setExpire(policy.getExpire());
										  //将VO类依次添加到集合中
										  listInfo.add(policyInfoVo);
									}
								}
							}
						}
					} else {
						//初始化一个保险单集合，设定为空 
						
					   //未输入姓名则返回所有
						List<Policy>  list1 = policyMapper.selectByExample(null);
						 if (list1!=null) {
							  for (int i = 0; i < list1.size(); i++) {
								//初始化一个VO类
								PolicyInfoVo policyInfoVo = new PolicyInfoVo();  
								  
								String policyno = list1.get(i).getPolicyno();
								Integer insuranceid = list1.get(i).getInsuranceid();
								Date expire = list1.get(i).getExpire();
								Date insure = list1.get(i).getInsure();
								String identity = list1.get(i).getIdentity();
								
								InsuredExample insuredExample = new InsuredExample();
								Criteria criteria = insuredExample.createCriteria();
								criteria.andIdentityEqualTo(identity);
								Insured insured=new Insured();
								List<Insured> insuredlist = insuredMapper.selectByExample(insuredExample);
								if (insuredlist!=null&&insuredlist.size()>0) {
									insured=insuredlist.get(0);
								}  
								String insuredname2 = insured.getInsuredname();
								String insuredphone = insured.getInsuredphone();
								
								Insurance insurance = insuranceMapper.selectByPrimaryKey(insuranceid);
								String insurancetype = insurance.getInsurancetype();
								policyInfoVo.setExpire(expire);
								policyInfoVo.setInsure(insure);
								policyInfoVo.setInsuredname(insuredname2);
								policyInfoVo.setInsurancetype(insurancetype);
								policyInfoVo.setInsuredphone(insuredphone);
								policyInfoVo.setPolicyno(policyno);
								listInfo.add(policyInfoVo);
								  
							}     
							  
						}
					
					}
		    
				 
				  
				 
				  
		 
		  
		  //返回这个集合
		return listInfo;
	}
	
	
	
	

}
